package com.richapp.launcher.serialport.infrared;

import android.content.Context;
import android.util.Log;

import com.richapp.launcher.serialport.infrared.abstracts.AMessage;
import com.richapp.launcher.serialport.infrared.interfaces.IMessage;
import com.richapp.launcher.serialport.infrared.interfaces.InfraredMessageListener;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPort;

/**
 * Created by jack on 2015/4/2 0002.
 */
public class InfraredServer {

    private Context context;
    private static InfraredServer infraredServer = null;
    private SerialPort serialPort = null;

    //老板子 /dev/ttyS5
    private String deviceName = "/dev/ttyS3";

    private int deviceBit  = 57600;
    public int deviceFlag = 0;

    private DataInputStream dataInputStream = null;
    private DataOutputStream dataOutputStream = null;

    private InfraredRunThread infraredRunThread = null;
    private InfraredMessageListener infraredMessageListener = null;

    private static final String TAG = InfraredServer.class.getName();

    private InfraredServer(){

    }

    public static InfraredServer getInstance(){
        if(infraredServer == null){
            infraredServer = new InfraredServer();
        }
        return infraredServer;
    }

    public void start(){
        try{
            serialPort = new SerialPort(new File(deviceName),deviceBit,deviceFlag);
            dataInputStream = new DataInputStream(serialPort.getInputStream());
            dataOutputStream = new DataOutputStream(serialPort.getOutputStream());
            infraredRunThread = new InfraredRunThread();
            infraredRunThread.setRunFlag(true);
            new Thread(infraredRunThread).start();
            Log.d(TAG,"启动红外服务");
        }catch (Exception e){
            Log.d(TAG,"启动红外串口错误:"+e.getMessage());
        }
    }

    public void stop(){
        try{
            infraredRunThread.setRunFlag(false);
            dataOutputStream.close();
            dataInputStream.close();
            serialPort.close();
        }catch (Exception e){
            Log.d(TAG,"关闭红外串口错误:"+e.getMessage());
        }finally {
            infraredRunThread = null;
            dataOutputStream = null;
            dataInputStream = null;
            serialPort = null;
        }
    }

    public void send(IMessage iMessage){
        if(iMessage instanceof AMessage){
            AMessage aMessage = (AMessage)iMessage;
            aMessage.setDataInputStream(dataInputStream);
            aMessage.setDataOutputStream(dataOutputStream);
            aMessage.setInfraredMessageListener(infraredMessageListener);
            infraredRunThread.addMessage(iMessage);
        }
    }

    public InfraredMessageListener getInfraredMessageListener() {
        return infraredMessageListener;
    }

    public void setInfraredMessageListener(InfraredMessageListener infraredMessageListener) {
        this.infraredMessageListener = infraredMessageListener;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public int getDeviceBit() {
        return deviceBit;
    }

    public void setDeviceBit(int deviceBit) {
        this.deviceBit = deviceBit;
    }

    public int getDeviceFlag() {
        return deviceFlag;
    }

    public void setDeviceFlag(int deviceFlag) {
        this.deviceFlag = deviceFlag;
    }
}
